package view.code
{
	import air.update.ApplicationUpdaterUI;

	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.hexagonstar.util.debug.Debug;

	import flash.net.SharedObject;
	import flash.utils.getQualifiedClassName;

	import iris.utils.IrisObjectUtil;

	import mvc.control.CharacterEvent;

	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.containers.TabNavigator;
	import mx.core.Application;
	import mx.events.DragEvent;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.managers.DragManager;

	import nz.co.codec.flexorm.EntityManager;

	import orm.fantasy.RCharItemVO;
	import orm.fantasy.TblCharacterVO;

	public class CodeCharacters extends HBox
	{
		public var tabCharacters:TabNavigator;

		private var _characterCollection:ArrayCollection;
		private var _oldIndex:int;

		public function CodeCharacters()
		{
			super();
			// PREINITIALIZE：初始化数据
			this.addEventListener(FlexEvent.PREINITIALIZE, design_preInitializeHandler);
			// INITIALIZE：追加Children
			this.addEventListener(FlexEvent.INITIALIZE, design_initializeHandler);
			// CREATION_COMPLETE：初始化画面
			this.addEventListener(FlexEvent.CREATION_COMPLETE, design_creationCompleteHandler);
		}

		protected function design_preInitializeHandler(event:FlexEvent):void
		{
			// 获取版本更新前的备份
			var so:SharedObject = SharedObject.getLocal("WowFantasy");
			Debug.trace("so.data.cache = " + so.data.cache);
			if(so.data.cache)
			{
				var characters:ArrayCollection = so.data.characters as ArrayCollection;
				var rCharItems:ArrayCollection = so.data.rCharItems as ArrayCollection;
				Debug.trace("get characters = " + characters.length + ", rCharItems = " + rCharItems.length);
				for each(var characterObj:Object in characters)
				{
					var tblCharacterVO:TblCharacterVO = IrisObjectUtil.copy(characterObj, new TblCharacterVO()) as TblCharacterVO;
					tblCharacterVO.id = 0;
					EntityManager.instance.save(tblCharacterVO);
				}
				for each(var rCharItemObj:Object in rCharItems)
				{
					var rCharItemVO:RCharItemVO = IrisObjectUtil.copy(rCharItemObj, new RCharItemVO()) as RCharItemVO;
					rCharItemVO.id = 0;
					EntityManager.instance.save(rCharItemVO);
				}
				so.data.cache = false;
				so.flush();
			}
			// 获得所有TblCharacterVO
			_characterCollection = EntityManager.instance.findAll(TblCharacterVO);
		}

		protected function design_initializeHandler(event:FlexEvent):void
		{
			// 为每个TblCharacterVO创建DesignCharacter
			for each(var vo:TblCharacterVO in _characterCollection)
			{
				var mvcEvent:CharacterEvent = new CharacterEvent(CharacterEvent.NEW_DESIGN_CHARACTER, vo);
				CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
			}
			this.tabCharacters.selectedIndex = 0;
		}

		protected function design_creationCompleteHandler(event:FlexEvent):void
		{
			var mvcEvent:CharacterEvent = new CharacterEvent(CharacterEvent.CHARACTERS_REFRESHED);
			CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
		}

		protected function tabCharacters_changeHandler(event:IndexChangedEvent):void
		{
			Application.application.dsnMain.dsnSelector.dgSelector.dataProvider = null;
			// 当选择“+”，弹出创建Character
			if(event.newIndex == this.tabCharacters.numChildren - 1)
			{
				var mvcEvent:CharacterEvent = new CharacterEvent(CharacterEvent.ADD_CHARACTER);
				mvcEvent.data = {oldIndex: event.oldIndex};
				CairngormEventDispatcher.getInstance().dispatchEvent(mvcEvent);
			}
		}

		protected function tabCharacters_dragExitHandler(event:DragEvent):void
		{
		}

		protected function tabCharacters_dragEnterHandler(event:DragEvent):void
		{
			DragManager.acceptDragDrop(this);
		}
	}
}